
var vm = new Vue({
	el:'#rrapp',
	data:{
	    q:{
            searchName: null,
            sidx: null,
            order: null
        },
        activeName: 'first',
        page:{total:0,limit:30,page:1},
        dataList:[],
        isadd: false,
		showList: true,
		showView: false,
		title: null,
        addFlag: false,
        editFlag: false,
		entSp: {"spLevel":1,"status":1},
        columnsList: [
          {name: 'SP名称', field: 'spName' , isShow: true},
          {name: 'SP接入号', field: 'spNumber' , isShow: true},
          {name: '邮箱', field: 'email' , isShow: true},
          {name: '手机号码', field: 'mobile' , isShow: true},
          {name: 'sp等级', field: 'spLevel' , isShow: true},
          {name: '状态  ', field: 'status' , isShow: true},
        ],
        spLevelList: [
        {'value': 1, 'text': '标准'},
        {'value': 2, 'text': '银卡'},
        {'value': 3, 'text': '金卡'},
        {'value': 4, 'text': '钻石'}
        ],
        spLevelMap: {},
        spLevelSearchList: {},
        statusList: [
        	{'value': 1, 'text': '正常'},
            {'value': 0, 'text': '禁用'}
        ],
        statusMap: {},
        statusSearchList: {},
		rules: {
          spName: [
            {required: true, message: '请输入SP名称', trigger: 'blur'},
            {min: 2, max: 20, message: '长度在 2 到 20 个字', trigger: 'blur'}
          ],
          test:[]
        },
        userRules: {
            userName: [
                {required: true, message: '请输入用户名', trigger: 'blur'},
                {min: 2, max: 20, message: '长度在 2 到 20 个字', trigger: 'blur'}
            ],
            password: [
                {required: true, message: '请输入密码', trigger: 'blur'},
                {min: 6, max: 20, message: '长度在 6 到 20 个字', trigger: 'blur'}
            ],
        },
        userEditRules: {
            userName: [
                {required: true, message: '请输入用户名', trigger: 'blur'},
                {min: 2, max: 20, message: '长度在 2 到 20 个字', trigger: 'blur'}
            ]
        },
        addUserView: false,
        editUserView: false,
        showUserView:false,
        showUserList:true,
        spUser: {},
        spUserEdit: {userType:1,status:1},
        userColumnsList: [
            {name: 'ID', field: 'userId' , isShow: true},
            {name: '用户名', field: 'userName' , isShow: true},
            {name: '用户类别', field: 'userType' , isShow: true},
            {name: '状态  ', field: 'status' , isShow: true}
        ],
        userTypeList: [
            {'value': 1, 'text': 'SP主账户'},
            {'value': 2, 'text': 'SP副账户'}
        ],
        userTypeMap: {},
        spUserList:[]
	},
    mounted: function () {
        this.spLevelMap = list2Map(this.spLevelList)
        this.statusMap = list2Map(this.statusList)
        this.userTypeMap = list2Map(this.userTypeList)
        this.getDataList();

    },
	methods: {
		query: function () {
			vm.reload();
		},
        getDataList:function () {
            this.q.page = this.page.page;
            this.q.limit = this.page.limit;

            $.ajax({
                type: "POST",
                url: "../entsp/list",
                data: JSON.stringify(this.q),
                success: function (r) {
                    if (r.code == 0) {
                        //debugger;
                        vm.dataList = r.data.list;
                        vm.page.total = r.data.totalCount;
                    } else {
                        toastr.error(r.message);
                    }
                }
            });
        },
        getUserList:function (spId) {
            this.q.page = this.page.page;
            this.q.limit = this.page.limit;
            this.q.spId = spId;
            $.ajax({
                type: "POST",
                url: "../spuser/list",
                data: JSON.stringify(this.q),
                success: function (r) {
                    if (r.code == 0) {
                        //debugger;
                        vm.spUserList = r.data.list;
                        vm.page.total = r.data.totalCount;
                    } else {
                        toastr.error(r.message);
                    }
                }
            });
        },
		add: function(){
		    vm.isadd = true; 
			vm.showList = false;
			vm.showView = false;
			vm.title = "新增";
			vm.entSp = {"spLevel":1,"status":1};
		},
        addUser: function(){
            vm.addUserView = true;
            vm.spUser= {"userType":1,"status":1};
            vm.spUser.spId = vm.entSp.spId;
        },
        saveUser: function () {
            this.$refs['addUserForm'].validate(vm.saveUserValid);
        },
        saveUserValid:function (valid){
            if (valid) {
                var url = "../spuser/save";
                $.ajax({
                    type: "POST",
                    url: url,
                    data: JSON.stringify(vm.spUser),
                    success: function(r){
                        if(r.code === 0){
                            toastr.success('操作成功');
                            vm.getUserList(vm.entSp.spId);
                            vm.addUserView = false
                        }else{
                            alert(r.message);
                        }
                    }
                });
            }else{
                alert('验证未通过');
            }
        },
        editUser: function (item) {
            vm.editUserView = true;
            vm.spUserEdit = {userType:1,status:1};

            vm.spUserEdit.spId = item.spId;
            vm.spUserEdit.userId = item.userId;
            vm.spUserEdit.userName = item.userName;
            vm.spUserEdit.mobile = item.mobile;
            vm.spUserEdit.userType = item.userType;
            vm.spUserEdit.status = item.status;
            vm.spUserEdit.password = "";

        },
        updateUser: function () {
            this.$refs['editUserForm'].validate(vm.updateUserValid);
        },
        updateUserValid:function (valid){
            if (valid) {
                var url = "../spuser/update";
                $.ajax({
                    type: "POST",
                    url: url,
                    data: JSON.stringify(vm.spUserEdit),
                    success: function(r){
                        if(r.code === 0){
                            toastr.success('操作成功');
                            vm.getUserList(vm.entSp.spId);
                            vm.editUserView = false
                        }else{
                            alert(r.message);
                        }
                    }
                });
            }else{
                alert('验证未通过');
            }
        },

        delOneUser: function (userId) {
            var userIds = [];
            userIds[0] = userId;

            this.$confirm('确定要删除该用户？','信息')
            .then(function(r){
                $.ajax({
                    type: "GET",
                    url: "../spuser/delete/"+userId,
                    success: function(r){
                        if(r.code == 0){
                            toastr.success('操作成功');
                            vm.getUserList(vm.entSp.spId);
                        }else{
                            alert(r.message);
                        }
                    }
                });
            })
            .catch(function(r){
                alert(r.message);
            });
        },


		update: function (spId) {
			if(spId == null){
				return ;
			}
			vm.showList = false;
			vm.showView = false;
            vm.title = "修改";
            vm.getInfo(spId)
		},
        view2update: function () {
            vm.showList = false;
            vm.showView = false;
            vm.title = "修改";
        },
		view: function(spId){
            vm.title = "查看";
	        vm.getInfo(spId);
            vm.getUserList(spId);
            vm.showList = false;
            vm.showView = true;
        },
        update2view: function(){
            vm.showView = true;
        },
        changeToUpdate:function(){
            vm.showView = false;
        },
		saveOrUpadateValid:function (valid){
            if (valid) {
                var url = vm.isadd ? "../entsp/save" : "../entsp/update";
                $.ajax({
				type: "POST",
			    url: url,
			    data: JSON.stringify(vm.entSp),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.message);
					}
				}
			});
            }else{
                alert('验证未通过');
            }
        },
		saveOrUpdate: function (event) {
			this.$refs['entSpForm'].validate(vm.saveOrUpadateValid);
		},
		del: function (spIds) {

			if(spIds == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../entsp/delete",
				    data: JSON.stringify(spIds),
				    success: function(r){
						if(r.code == 0){
                            alert('删除成功', function(index){
                                vm.reload();
                            });
						}else{
							alert(r.message);
						}
					}
				});
			});
		},
		delOne: function (spId) {

            var spIds = [];
            spIds[0] = spId;

        	confirm('确定要删除该记录？', function(){
        		$.ajax({
        			type: "GET",
        			url: "../entsp/delete/"+spId,
        			success: function(r){
                        if(r.code == 0){
                            alert('删除成功', function(index){
                                vm.reload();
                            });
                        }else{
                            alert(r.message);
                        }
        			}
        		});
        	});
        },
		getInfo: function(spId){
			$.get("../entsp/info/"+spId, function(r){
                vm.entSp = r.data;
            });
		},
        handleCommand:function (commond) {
            if(commond=='d')
            {
                location.href = "../entsp/download?query=" + escape(JSON.stringify(vm.q));
                return;
            }

            if(vm.total>5000)
            {
                toastr.error("导出数据条数不能超过5000条,请用sql从数据库导出!");
                return;
            }

            if(commond=='a')
            {
                $("#data").tableExport({type:'csv'},'data');
                return;
            }

            if(commond=='b')
            {
                $("#data").tableExport({type:'txt'},'data');
                return;
            }

            if(commond=='c')
            {
                $("#data").tableExport({type:'excel',escape:false,
                    mso: {fileFormat:'xlsx',
                        excelstyles: ['border-bottom', 'border-top', 'border-left', 'border-right'],
                        worksheetName: ['Data']}},'data');
                return;
            }
        },
        exportExl: function () {
            if(vm.total>5000)
            {
                toastr.error("导出数据条数不能超过5000条,请用sql从数据库导出!");
                return;
            }

            if(vm.total>5000)
            {
                toastr.error("导出数据条数不能超过5000条,请用sql从数据库导出!");
                return;
            }

            $("#data").tableExport({type:'excel',escape:false,
                mso: {fileFormat:'xlsx',
                    excelstyles: ['border-bottom', 'border-top', 'border-left', 'border-right'],
                    worksheetName: ['Data']}},'data');

        },

        pageSizeChange:function (pageSize) {
            this.page.limit = pageSize;
            this.reload();
        },
        pageCurrentChange:function (page) {
            this.page.page = page;
            this.reload();
        },
        pageChange:function () {
            this.reload();
        },
		reload: function (event) {
			this.isadd = false;
			this.showList = true;
            this.getDataList();
		}
	}
});
